/**
 * @Author: Bobby
 * @Date: 2022-02-15 13:14:17
 * @LastEditTime: 2022-02-18 14:05:59
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\project\bobby\store\diyInteraction.js
 * @CIMC-V2
 */
import { removeModule } from '@ve/utils/utils'
/**
 * @description DIY-交互
 */
const state = () => ({
  modal: false, //弹框
  source: '', //来源
  cacheInteractions: [], //临时缓存交互数组
  esPaneTabsActive: '1', //tabs选择
  selectEventId: '', //当前选择事件ID
  selectEventItem: {}, //当前选择事件
  selectActionId: '', //当前选择动作ID
  selectActionItem: {}, //当前选择动作
})
const getters = {
  modal: (state) => state.modal,
  getSource: (state) => state.source,
  getCacheInteractions: (state) => state.cacheInteractions,
  getEsPaneTabsActive: (state) => state.esPaneTabsActive,
  //事件
  getSelectEventId: (state) => state.selectEventId,
  getSelectEventItem: (state) => state.selectEventItem,
  //动作
  getSelectActionId: (state) => state.selectActionId,
  getSelectActionItem: (state) => state.selectActionItem,
}
const mutations = {
  reset(state) {
    state.cacheInteractions = []
    state.esPaneTabsActive = '1'
    state.source = ''
    state.selectEventId = ''
    state.selectEventItem = {}
    state.selectActionId = ''
    state.selectActionItem = {}
  },
  //交互-弹框开关
  modalSwitch(state) {
    state.modal = !state.modal
  },
  //获取临时缓存交互数据
  setCacheInteractions(state, data) {
    state.cacheInteractions = data
  },
  //tabs切换
  setEsPaneTabsActive(state, type) {
    state.esPaneTabsActive = type
  },

  //获取事件id
  setSelectEventId(state, id) {
    state.selectEventId = id
  },
  //获取事件
  setSelectEventItem(state, data) {
    state.selectEventItem = data
  },
  //获取动作id
  setSelectActionId(state, id) {
    state.selectActionId = id
  },
  //获取动作
  setSelectActionItem(state, data) {
    state.selectActionItem = data
  },
  //添加动作配置
  addActionItemExtension(state, data) {
    let _actions = state.selectEventItem.actions
    if (_actions) {
      _actions.push(data)
      state.selectEventItem.actions = _actions
    }
  },
  //删除事件
  removeEvent(state, item) {
    let _list = removeModule(state.cacheInteractions, item._id, '_id')
    //先更新当前页面
    state.cacheInteractions = _list
  },
  //删除动作
  removeAction(state, item) {
    let _list = removeModule(state.cacheInteractions, item._id, '_id', 'actions', 'elseActions')
    //先更新当前页面
    state.cacheInteractions = _list
  },
  //设置来源
  setSource(state, name) {
    state.source = name
  },
}
const actions = {
  reset({ commit }) {
    commit('reset')
  },
  //获取页面信息
  modalSwitch({ commit }) {
    commit('modalSwitch')
  },
  //获取临时缓存交互数据
  setCacheInteractions({ commit }, data) {
    commit('setCacheInteractions', data)
  },
  //切换tabs
  setEsPaneTabsActive({ commit }, type) {
    commit('setEsPaneTabsActive', String(type))
  },
  //获取事件id
  setSelectEventId({ commit }, id) {
    commit('setSelectEventId', id)
  },
  //获取事件
  setSelectEventItem({ commit }, data) {
    commit('setSelectEventItem', data)
  },
  //获取动作id
  setSelectActionId({ commit }, id) {
    commit('setSelectActionId', id)
  },
  //获取动作
  setSelectActionItem({ commit }, data) {
    console.log(data)
    commit('setSelectActionItem', data)
  },
  addActionItemExtension({ commit }, data) {
    commit('addActionItemExtension', data)
  },
  //删除事件
  removeEvent({ commit }, item) {
    commit('removeEvent', item)
  },
  //删除动作
  removeAction({ commit }, item) {
    commit('removeAction', item)
  },
  //来源
  setSource({ commit }, name) {
    commit('setSource', name)
  },
}
export default { state, getters, mutations, actions }
